* Compliance in Time: Lessons from the Inter-American Court of Human Rights
*  Aníbal Pérez-Liñán, University of Notre Dame
*  Luis Schenoni, University College London
*  Kelly Morrison, University of Tennessee, Knoxville
*  Replication file - 11/25/2022

use "0_iacthr.dta", clear

*** Figure 3. Life Cycle (Full Compliance) and Predicted YPC for Alternative Polynomials ***
*    Model specifications - t^1, ..., t^9
global spec1   c.t                           
global spec2   c.t##c.t                      
global spec3   c.t##c.t##c.t                
global spec4   c.t##c.t##c.t##c.t            
global spec5   c.t##c.t##c.t##c.t##c.t       
global spec6   c.t##c.t##c.t##c.t##c.t##c.t  
global spec7   c.t##c.t##c.t##c.t##c.t##c.t##c.t  
global spec8   c.t##c.t##c.t##c.t##c.t##c.t##c.t##c.t  
global spec9   c.t##c.t##c.t##c.t##c.t##c.t##c.t##c.t##c.t

*  Estimate Models
forvalues T = 1/9 {
 local spec spec`T'
 mecloglog y_full $`spec'
 estimates store `spec'
}

* Create Figure 3
preserve
sort t
 collapse y_full, by(t)
 forvalues T = 1/9 {
  est restore spec`T'
   predict p`T'
   format %8.1f y_full p*
   twoway (line y_full t, title("Order `T'") ylabel(#3, format(%8.1f)) lc(orange)) (line p`T' t, legend(off) lc(black) ) 
   graph save pred`T'.gph, replace 
 } 
graph combine pred1.gph pred2.gph pred3.gph pred4.gph pred5.gph pred6.gph pred7.gph pred8.gph pred9.gph, rows(3) xsize(7) ysize(6) ycommon
graph save Figure3.gph, replace
restore
  foreach g in pred1.gph pred2.gph pred3.gph pred4.gph pred5.gph pred6.gph pred7.gph pred8.gph pred9.gph {
  	erase `g'
  }

  
*** Figure 4. Root Mean Squared Error for Alternative Transformations of Time ***

* Dataset for Cross-Validation repeats
  ** Warning: this loop takes about an hour to run **
putexcel set RMSE.xlsx, replace
putexcel A1="outcome" B1="repeat" C1="order" D1="fold" E1="rmse"  
local row = -3

foreach v in part full any {
forvalues rep = 1/50 {
 local order 
 forvalues T = 1/9 {
  gen t`T' = t^`T'
  local order `order' t`T'
  crossfold mecloglog y_`v' `order' , k(5) stub(fold)
  mat A = r(fold)
  local row = 5 + `row'
  putexcel E`row'=matrix(A)
  forvalues r2 = 0/4 {
  	local row2 = `row' + `r2'
    putexcel C`row2'=`T'
	putexcel D`row2'= `r2'+1
	putexcel B`row2'= `rep'
	putexcel A`row2'="`v'"
    }
 } 
drop t1-t9
local order
}
}

* Summary & Figure 4
import excel "RMSE.xlsx", sheet("Sheet1") firstrow clear
 gen     outcome2 = 1 if outcome == "part"
 replace outcome2 = 2 if outcome == "full"
 replace outcome2 = 3 if outcome == "any"
  label define o2 1 "Partial" 2 "Full" 3 "First"
   label values outcome2 o2
   format %10.3f rmse
 reg rmse i.order#i.outcome2
  margins, at(outcome2=1 order=(1(1)9)) 
    marginsplot, title("4.1. Partial Compliance", size(large)) xtitle("Polynomial's order") ytitle("RMSE") plotopts(m(none)) ylabel(,format(%10.3f))
    graph save poly1.gph, replace
   margins, at(outcome2=2 order=(1(1)9)) 
    marginsplot, title("4.2. Full Compliance", size(large)) xtitle("Polynomial's order") ytitle("RMSE") plotopts(m(none)) ylabel(,format(%10.3f))
    graph save poly2.gph, replace
   margins, at(outcome2=3 order=(1(1)9)) 
    marginsplot, title("4.3. First Compliance", size(large)) xtitle("Polynomial's order") ytitle("RMSE") plotopts(m(none)) ylabel(,format(%10.3f))
    graph save poly3.gph, replace
  graph combine poly1.gph poly2.gph poly3.gph,  rows(1) xsize(7) ysize(3)
  graph save Figure4.gph, replace
  foreach g  in poly1.gph poly2.gph poly3.gph RMSE.xlsx {
  	erase `g'
  }
 

*** Table 1. Discrete-Time Models of Compliance with the IACtHR ***

** First compliance
mecloglog y_any i.rtype2 c.t##c.t##c.t##c.t##c.t##c.t  || cowcode: || casecode:
 est store MP1
 predict ypc_any if e(sample)

* Average time to compliance
 margins, post 
  nlcom 1/_b[_cons]
  global tfirst = 1/_b[_cons]
est restore MP1  

** Full compliance
mecloglog y_full i.rtype2 c.t##c.t##c.t##c.t##c.t##c.t  || cowcode: || casecode:
 est store MF1
 predict ypc_full if e(sample)

* Average time to compliance
 margins, post 
  nlcom 1/_b[_cons]
  global tfull = 1/_b[_cons]
  est restore MF1
 
  * Table 1
  esttab  MP1 MF1 /// 
      using "Table_1.rtf", replace wide  b(%9.2f) se	///  
	  	  starlevels(* 0.05) nobase noomit label nodepvars  ///
	      title("Table 1. Discrete-Time Models of Compliance with the IACtHR")  
 
 
*** Table 2. Four Metrics of Compliance, by Type of Measure ***

* Discrete-time metrics 
preserve
sort repcode // Aggregate first by reparation, then by type
 collapse (mean) ypc_any ypc_full (first) rtype2, by(repcode)
  sort rtype2
   collapse (mean) ypc_any ypc_full, by(rtype2)
 gen etc_any  = 1/ypc_any 
 gen etc_full = 1/ypc_full
 save discrete, replace
restore
* Snapshots metrics
preserve
 sort repcode year
 gen     Tany = 0 
 replace Tany = t if y_any == 1
 gen     Tfull = 0
 replace Tfull = t if y_full == 1
 collapse (last) rtype2 (max) y_any Tany y_full Tfull, by(repcode)
  replace    Tany = . if Tany == 0
   summarize Tany, meanonly
   global    Tany = r(mean)
  replace    Tfull = . if Tfull == 0
   summarize Tfull, meanonly
   global    Tfull = r(mean)
 gen reparations = 1  
 sort rtype2
 collapse (sum) reparations y_any y_full (mean) Tany Tfull, by(rtype2)
* Merge files for snapshot and discrete-time
 joinby rtype2 using discrete  
* Create table  - Published version does not include figures for full compliance
  replace y_any = 100*y_any/reparations
  replace y_full = 100*y_full/reparations
  format %8.1f y_any y_full
  format %8.0f Tany Tfull etc_any etc_full
  format %8.3f ypc_any ypc_full
  drop reparations
  order rtype2 y_any ypc_any Tany etc_any y_full ypc_full Tfull etc_full
  label var y_any "Rate, %" 
  label var Tany "Time"
  label var ypc_any "YPC"
  label var etc_any "ETC"
  label var y_full "Rate (full, %)" 
  label var Tfull "Time (full)"
  label var ypc_full "YPC (full)"
  label var etc_full "ETC (full)"
  label var rtype2 "Subtype"
  label values rtype2 rtype2
  label values rtype2 rtype2
  sort etc_full
  tostring y_any y_full, replace force format(%8.1f)
  tostring Tany Tfull etc_any etc_full, replace force format(%8.0f)
  tostring ypc_any ypc_full, replace force format(%8.3f)
  destring _all, replace
  export excel using "Table2.xlsx", firstrow(varlabels) replace
restore
 erase discrete.dta


*** Figure 5. Life Cycle of Full Compliance, by Type of Measure ***

* Models with full compliance
mecloglog y_full c.t##c.t##c.t##c.t##c.t##c.t if rtype3 == 0 || cowcode: || casecode: 
 est store MF2
mecloglog y_full c.t##c.t##c.t##c.t##c.t##c.t if rtype3 == 1 || cowcode: || casecode: 
 est store MF3

est restore MF2
  margins, at(t=(0(1)20))
  marginsplot, noci recast(line) ytitle("Probability of full compliance") title("") ///
               title("5.1. Immediate remedies") saving(L1, replace)  
est restore MF3
  margins, at(t=(0(1)20))
  marginsplot, noci recast(line) ytitle("Probability of full compliance") title("") ///
              title("5.2. Protracted remedies")  saving(L2, replace)

 * Figure 5
 graph combine L1.gph L2.gph, cols(2) xcommon ycommon xsize(11) ysize(6) saving(Figure5, replace) 
  erase L1.gph 
  erase L2.gph
 